package com.base.cn.platform.os.common.service;

import com.base.cn.platform.os.common.mybatis.BaseEntity;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.manage.subject.SysSubject;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 公共设置专业数据的Service
 *
 * @author s.li
 * @create 2018-04-19-18:22
 */
@Service
public class CommonSetSubjectDataService<E extends BaseEntity> {

    /**
     * 设置专业数据
     *
     * @param e           数据对象
     * @param subjectData 是否设置专业，true设置，false不设置
     * @return E
     */
    public E setSubjectData(SubjectService subjectService, E e, boolean subjectData) {
        if (ObjectUtils.isNotEmpty(e) && subjectData) {
            List<E> list = new ArrayList<>();
            list.add(e);
            list = this.setSubjectData(subjectService, list, subjectData);
            e = list.get(0);
        }
        return e;
    }

    /**
     * 设置专业数据
     *
     * @param list           数据列表
     * @param setSubjectData 是否设置专业，true设置，false不设置
     * @return List<E>
     */
    public List<E> setSubjectData(SubjectService subjectService, List<E> list, boolean setSubjectData) {
        if (ObjectUtils.isNotEmpty(list) && setSubjectData) {
            String subjectIds = this.getSubjectIds(list);
            if(StringUtils.isNotEmpty(subjectIds)){
                Map<String, Object> map = new HashMap<>();
                map.put("queryIds", subjectIds);
                Map<BigDecimal, SysSubject> subjectMaps = subjectService.findSubjectToIdMap(map);
                this.setSubjectData(subjectMaps,list);
            }
        }
        return list;
    }

    /**
     * 获取专业ID串（去重的）
     * @param list
     * @return 专业ID串
     */
    public String getSubjectIds( List<? extends BaseEntity> list){
        Set<String> idSet =new HashSet<>();
        list.forEach(e->{
            if(StringUtils.isNotEmpty(e.getSubjectIds())){
                String[] arr = e.getSubjectIds().split("\\*");
                Arrays.stream(arr).forEach(str->{
                    if(StringUtils.isNotEmpty(str)){
                        String[] _arr = StringUtils.subHeadTailString(str,",").split(",");
                        Arrays.stream(_arr).forEach(id->{
                            if(StringUtils.isNotEmpty(id)){
                                idSet.add(id);
                            }
                        });
                    }
                });
            }
        });
        String subjectIds ="";
        if(ObjectUtils.isNotEmpty(idSet)){
            subjectIds = idSet.stream().collect(Collectors.joining(","));
        }
        return subjectIds;
    }
    /**
     * 设置专业数据
     * @param subjectMaps
     * @param list
     */
    public void setSubjectData(Map<BigDecimal, SysSubject> subjectMaps,List<? extends BaseEntity> list){
        if (ObjectUtils.isNotEmpty(subjectMaps)) {
            list.forEach(e -> {
                if(StringUtils.isNotEmpty(e.getSubjectIds())){
                    String[] subjectIdsArr = e.getSubjectIds().split("\\*");
                    List<SysSubject> subjectList = new ArrayList<>();
                    Arrays.stream(subjectIdsArr).forEach(subjectIds->{
                        subjectIds = StringUtils.subHeadTailString(subjectIds,",");
                        if(StringUtils.isNotEmpty(subjectIds)){
                            String[] idArr = subjectIds.replaceAll(",,",",").split(",");
                            SysSubject parentSubject = new SysSubject();
                            List<SysSubject> childSubjectList = new ArrayList<>();
                            IntStream.range(0,idArr.length).forEach(index->{
                                SysSubject subject = subjectMaps.get(new BigDecimal(idArr[index]));
                                if(ObjectUtils.isNotEmpty(subject)){
                                    if(index==0){
                                        parentSubject.setId(subject.getId());
                                        parentSubject.setSubjectName(subject.getSubjectName());
                                    }else{
                                        childSubjectList.add(subject);
                                    }
                                }
                            });
                            if(DataUtil.idIsNotNull(parentSubject.getId())){
                                parentSubject.setChildSubjectList(childSubjectList);
                                subjectList.add(parentSubject);
                            }
                        }
                    });
                    e.setSubjectList(subjectList);
                }
            });
        } else {
            list.forEach((v) -> v.setSubjectList(Collections.emptyList()));
        }
    }
}
